package file;

import java.io.File;
import java.util.Scanner;

//查找文件，并询问用户是否要删除
public class Demo1 {
    public static void main(String[] args) {
        System.out.println("请输入要查询的目录：");
        Scanner scan = new Scanner(System.in);
        String find =scan.next();
        File file = new File(find);
        if(!file.isDirectory()) {
            System.out.println("你输入的不是目录或者该目录不存在....");
            return;
        }
        System.out.println("请输入你要指定的关键字：");
        String token = scan.next();
        scanFile(file, token);
    }

    private static void scanFile(File rootFile, String token) {
        File[] files = rootFile.listFiles();
        for(File file : files) {
            if(file.isDirectory()) {
                scanFile(file, token);
            } else {
                if(file.getName().contains(token)) {
                    System.out.println("该文件名包含你指定发关键字：" + file.getAbsolutePath() + "请选择是否要进行删除（Y/N）");
                    Scanner scan = new Scanner(System.in);
                    String ret = scan.next();
                    if(ret.equals("Y")) {
                        file.delete();
                    }
                }
            }
        }
    }
}
